package com.cjm.algorithm.leetcode;

import com.cjm.algorithm.TreeNode;

public class IsValidBST98 {
    long pre = Long.MIN_VALUE;
    public boolean isValidBST(TreeNode root) {
        return extracted(root);
    }

    private boolean m1(TreeNode root, long minValue) {
        if (root == null) {
            return true;
        }
        if (!m1(root.left, minValue)) {
            return false;
        }
        if (root.val <= minValue) {
            return false;
        }
        minValue = root.val;
        return m1(root.right, minValue);
    }

    private boolean extracted(TreeNode root) {
        if (root == null) return true;
        // 左
        if (!isValidBST(root.left)) {
            return false;
        }
        // 中
        if (root.val<=pre) {
            return false;
        }
        pre = root.val;
        // 右
        return isValidBST(root.right);
    }


    public static void main(String[] args) {
        TreeNode treeNode = TreeNode.generatorTreeNode(new Integer[]{1,1});
        IsValidBST98 isValidBST98 = new IsValidBST98();
        System.err.println(isValidBST98.isValidBST(treeNode));
    }
}
